package com.java110.mall.shop.bmo.convenienceMenus.impl;

import com.java110.dto.convenienceMenus.ConvenienceMenusDto;
import com.java110.dto.file.FileRelDto;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.intf.shop.IConvenienceMenusInnerServiceSMO;
import com.java110.mall.shop.bmo.convenienceMenus.IGetConvenienceMenusBMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("getConvenienceMenusBMOImpl")
public class GetConvenienceMenusBMOImpl implements IGetConvenienceMenusBMO {

    @Autowired
    private IConvenienceMenusInnerServiceSMO convenienceMenusInnerServiceSMOImpl;

    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;
    /**
     *
     *
     * @param  convenienceMenusDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> get(ConvenienceMenusDto convenienceMenusDto) {


        int count = convenienceMenusInnerServiceSMOImpl.queryConvenienceMenussCount(convenienceMenusDto);

        List<ConvenienceMenusDto> convenienceMenusDtos = null;
        if (count > 0) {
            convenienceMenusDtos = convenienceMenusInnerServiceSMOImpl.queryConvenienceMenuss(convenienceMenusDto);
            freshIcons(convenienceMenusDtos);
        } else {
            convenienceMenusDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) convenienceMenusDto.getRow()), count, convenienceMenusDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

    private void freshIcons(List<ConvenienceMenusDto> convenienceMenusDtos) {
        if (convenienceMenusDtos == null || convenienceMenusDtos.size() < 1) {
            return;
        }

        //输入
        List<String> productIds = new ArrayList<>();

        for (ConvenienceMenusDto tmpProductDto : convenienceMenusDtos) {
            productIds.add(tmpProductDto.getConvenienceMenusId());
        }

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjIds(productIds.toArray(new String[productIds.size()]));
        fileRelDto.setRelTypeCd(FileRelDto.REL_TYPE_CD_MENU_ICON);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);

        //刷入图片信息
        List<String> photoVos = null;
        String url = null;
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN,"IMG_PATH");
        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
        for (ConvenienceMenusDto tmpDto : convenienceMenusDtos) {
            for (FileRelDto tmpFileRelDto : fileRelDtos) {
                if (tmpDto.getConvenienceMenusId().equals(tmpFileRelDto.getObjId())) {
                    //url = "/callComponent/download/getFile/file?fileId=" + tmpFileRelDto.getFileRealName() + "&communityId=-1";
                    url = imgUrl + tmpFileRelDto.getFileRealName();
                    tmpDto.setIcon(url);
                }
            }
        }
    }
}
